package com.havey.ye.mybatisPlus;

import java.sql.Types;
import java.util.Collections;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;

public class CodeGenerator {
        /**
         * 数据源配置
         */
        private static final String URL = "jdbc:mysql://localhost:3306/ye_aigc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
        /**
         * mysql用户名
         */
        private static final String USERNAME = "root";
        /**
         * mysql密码
         */
        private static final String PASSWORD = "926926";

        public static void main(String[] args) {
                FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                                .globalConfig(builder -> {
                                        builder.author("yehuaiwei") // 设置作者
                                                        .enableSwagger() // 开启 swagger 模式
                                                        .outputDir("/Users/yehuaiwei/workspace/idea/AIGC/middle/springAI/src/main/java/"); // 指定输出目录
                                })
                                .dataSourceConfig(builder -> builder
                                                .typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                                                        int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                                                        if (typeCode == Types.SMALLINT) {
                                                                // 自定义类型转换
                                                                return DbColumnType.INTEGER;
                                                        }
                                                        return typeRegistry.getColumnType(metaInfo);
                                                }))
                                .packageConfig(builder -> builder.parent("com.havey.ye") // 设置父包名
                                                // .moduleName("aigc") // 设置父包模块名
                                                .pathInfo(Collections.singletonMap(OutputFile.xml,
                                                                "/Users/yehuaiwei/workspace/idea/AIGC/middle/springAI/src/main/resources/mapper")) // 设置mapperXml生成路径
                                )
                                .strategyConfig(builder -> builder.addInclude("user_info") // 设置需要生成的表名
                                // .addTablePrefix("t_", "c_") // 设置过滤表前缀
                                )
                                // .templateEngine(new FreemarkerTemplateEngine()) //
                                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                                .execute();
        }
}